<?php
$loader->requireOnce('includes/inPatientCore/InPatientCoreErrorCode.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientCoreException.class.php');
$loader->requireOnce('includes/CoreErrorCode.class.php');
$loader->requireOnce('includes/CoreException.class.php');
$loader->requireOnce('includes/inPatientCore/InputServiceLoader.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientSurgery.class.php');
$loader->requireOnce('controllers/C_InPatientSEBase.class.php');
$loader->requireOnce('controllers/C_Surgery.class.php');
$loader->requireOnce('ordo/InPatient.class.php');



/**
 * C_InSurgery Processor.
 * @package core.includes.inPatientCore
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_InPatientSurgery extends C_InPatientSEBase  {
	private $logger ;
	private $surProcessor;


	/**
	 *
	 * @param $controller controller for build ui
	 */
	function __construct($template_mod = "general") {
		parent::__construct($template_mod);
		$this->logger = LoggerManager::getLogger('core.controllers.C_ServiceInput');
		$this->template_mod = $template_mod;
		$this->view->path="service_input";
		$this->surProcessor = new InPatientSurgery($this);
	}



	function actionAddSurgeryAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''), 'result'=>array());
		$ticketId = $_GET['ticketId'];
		$employees = $_GET['employees'];
		$roles = $_GET['roles'];
		$surgeryDate = $_GET['surgeryDate'];
		$surgeryServiceId = $_GET['surgeryServiceId'];

		$this->logger->info('Start actionAddSurgeryAjax');
		try {
			
			//$this->surProcessor->addSurgeryService($ticketId, $surgeryServiceId, $surgeryDate, $doctorId);
			$team = array();
			$i = 0;
			foreach ($employees as $e) {
				$team[$i] = array('id'=>$e,'employeeType'=>$roles[$i]);
				$i++;
				
			}			
			$this->surProcessor->addService($ticketId, $surgeryServiceId, $surgeryDate, $team);

		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('Load actionAddSurgeryAjax fail. Error detail: '. $ex->getMessage());
		} catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('Load actionAddSurgeryAjax fail. Error detail: '. $ex->getMessage());
        }
		$this->logger->info('Finish actionAddSurgeryAjax');
		return json_encode($result);

	}
	

	public function actionDeleteInPatientSurgeryAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''), 'result'=>array());
        $ticketId = $_GET['ticketId'];
        $surgeryId = $_GET['surgeryId'];

        $this->logger->info('Start actionDeleteInPatientSurgeryAjax');
        try {
            $this->surProcessor->deleteInPatientSurgery($ticketId, $surgeryId);

        } catch (InpatientCoreException $ex) {

            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('Load actionDeleteInPatientSurgeryAjax fail. Error detail: '. $ex->getMessage());
        } catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('Load actionDeleteInPatientSurgeryAjax fail. Error detail: '. $ex->getMessage());
        }
        $this->logger->info('Finish actionDeleteInPatientSurgeryAjax');
        return json_encode($result);

	}
	
	public function actionDisplaySurgeryTeam() {
		$result = array('error'=>array('code'=>0,'mess'=>''), 'result'=>array());
        $ticketId = $_GET['ticketId'];
        $surgeryId = $_GET['surgeryId'];

        $this->logger->info('Start actionDisplaySurgeryTeam');
        try {
            $result['result']['form'] = $this->surProcessor->buildInPatientSurgeryTeam($ticketId, $surgeryId);

        } catch (InpatientCoreException $ex) {

            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('Load actionDisplaySurgeryTeam fail. Error detail: '. $ex->getMessage());
        } catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('Load actionDisplaySurgeryTeam fail. Error detail: '. $ex->getMessage());
        }
        $this->logger->info('Finish actionDisplaySurgeryTeam');
        return json_encode($result);
		
		
	}
	
    public function actionDisplaySurgeryTeamTemplate() {
        $result = array('error'=>array('code'=>0,'mess'=>''), 'result'=>array());
        $ticketId = $_GET['ticketId'];
        $surgeryId = $_GET['surgeryId'];

        $this->logger->info('Start actionDisplaySurgeryTeamTemplate');
        try {
            $result['result']['form'] = $this->surProcessor->buildInPatientSurgeryTeamTemplate();

        } catch (InpatientCoreException $ex) {

            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('Load actionDisplaySurgeryTeamTemplate fail. Error detail: '. $ex->getMessage());
        } catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('Load actionDisplaySurgeryTeamTemplate fail. Error detail: '. $ex->getMessage());
        }
        $this->logger->info('Finish actionDisplaySurgeryTeamTemplate');
        return json_encode($result);
        
        
    }
	

   

}
?>